﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Common;
using System.Data;

/// <summary>
///DBImportantFans 的摘要说明
/// </summary>
public class DBImportantFans
{
	public DBImportantFans()
	{
		
	}

    public bool Add(string weiboAccount, string fansId)
    {
        DbCommand command = DBConfig.CreateCommand("Insert into [importantFans](weiboAccount, fansID) VALUES(@weiboAccount, @fansID)");
        command.Parameters.Add(DBConfig.CreateParameter("@weiboAccount", weiboAccount));
        command.Parameters.Add(DBConfig.CreateParameter("@fansID", fansId));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    public bool Delete(string weiboAccount, string fansId)
    {
        DbCommand command = DBConfig.CreateCommand("Delete From [importantFans] Where weiboAccount=@weiboAccount AND fansID=@fansID");
        command.Parameters.Add(DBConfig.CreateParameter("@weiboAccount", weiboAccount));
        command.Parameters.Add(DBConfig.CreateParameter("@fansID", fansId));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    public ImportantFansData Select(string weiboAccount, string fansId)
    {
        DbCommand command = DBConfig.CreateCommand("Select * From [importantFans] Where weiboAccount=@weiboAccount AND fansID=@fansID");
        command.Parameters.Add(DBConfig.CreateParameter("@weiboAccount", weiboAccount));
        command.Parameters.Add(DBConfig.CreateParameter("@fansID", fansId));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            if (reader.Read())
                return new ImportantFansData(fansId);
            else
                return null;
        }

    }

    public List<ImportantFansData> SelectList(string weiboAccount)
    {
        DbCommand command = DBConfig.CreateCommand("Select Top 300 fansID From [importantFans] Where weiboAccount=@weiboAccount");
        command.Parameters.Add(DBConfig.CreateParameter("@weiboAccount", weiboAccount));
        DbDataAdapter adapter = DBConfig.CreateDataAdapter();
        adapter.SelectCommand = command;
        DataTable table = new DataTable();
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            adapter.Fill(table);
        }

        List<ImportantFansData> result = new List<ImportantFansData>();
        for (int i = 0; i < table.Rows.Count; i++)
        {
            result.Add(new ImportantFansData(table.Rows[i][0].ToString()));
        }
        return result;
    }
}